package com.hspedu.hspliving.commodity.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.github.pagehelper.PageInfo;
import com.hspedu.common.valid.SaveGroup;
import com.hspedu.common.valid.UpdateGroup;
import com.hspedu.hspliving.commodity.dto.BrandDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hspedu.hspliving.commodity.entity.BrandEntity;
import com.hspedu.hspliving.commodity.service.BrandService;
import com.hspedu.common.utils.PageUtils;
import com.hspedu.common.utils.R;

import javax.validation.Valid;


/**
 * 家居品牌
 *
 * @author bing
 * @email bing@gmail.com
 * @date 2024-05-29 20:37:15
 */
@RestController
@RequestMapping("commodity/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    //@RequiresPermissions("commodity:brand:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageInfo<BrandEntity> brandEntityPageInfo = brandService.queryPage(params);
//
//        return R.ok().put("page", brandEntityPageInfo);
//    }
    @RequestMapping("/list")
    public R list(@RequestBody BrandDTO brandDTO){
        PageInfo<BrandEntity> brandEntityPageInfo = brandService.queryPage(brandDTO);
        return R.ok().put("page", brandEntityPageInfo);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:brand:info")
    public R info(@PathVariable("id") Long id){
		BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * 老韩解读
     * 1. @Validated 标注在这里表示启用对BrandEntity 字段的校验
     * 2. 注意: 如果不写@Validated 即使在BrandEntity 写了校验注解, 也不会生效
     * 3. BindingResult result: springboot 会将校验的结果放入到result
     * 4. 程序员可以通过BindingResult result 取出自己关系的错误信息
     * 5.@Validated({SaveGroup.class}) 表示在执行savp API 接口时， 走的是
     * SaveGroup.class 校验规则
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:brand:save")
    public R save(@RequestBody @Validated({SaveGroup.class}) BrandEntity brand){
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:brand:update")
    public R update(@RequestBody @Validated({UpdateGroup.class}) BrandEntity brand){
		if (brandService.updateById(brand)){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:brand:delete")
    public R delete(@RequestBody Long[] ids){
		brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
